package com.paisheng.justlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.paisheng.justlife.R;
import com.paisheng.justlife.adapter.RubbishListAdapter;
import com.paisheng.justlife.bean.Waste;
import com.paisheng.justlife.common.MenuType;
import com.paisheng.justlife.utils.SharedPreferencesHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 投递结束页面
 */
public class FinishActivity extends Activity implements View.OnClickListener {

    private Button btnFinish, btnContinue;
    private Intent intent;
    private TextView tvTime, tv_money;
    private int recLen = 60;
    Timer timer = new Timer();
    private ListView rubbishList;
    private SharedPreferencesHelper spfh;
    private String preference;
    private List<Waste> wasteList = new ArrayList<>();
    private String phone;
    private RubbishListAdapter rubbishListAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish);
        initview();

    }

    private void initview() {
        btnFinish = (Button) findViewById(R.id.btn_finish);
        btnContinue = (Button) findViewById(R.id.btn_continue);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tv_money = (TextView) findViewById(R.id.tv_money);
        rubbishList = (ListView) findViewById(R.id.lv_rubbish_list);
        btnFinish.setOnClickListener(this);
        btnContinue.setOnClickListener(this);
        tvTime.setText(recLen + "");
        intent = getIntent();
        phone = intent.getStringExtra("phone");
        spfh = new SharedPreferencesHelper(this, "CAST_MESSAGE");
        preference = (String) spfh.getSharedPreference(phone, String.class);
        wasteList = JSON.parseArray(preference, Waste.class);
        BigDecimal sum = BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_DOWN);
        for (Waste waste : wasteList) {
            sum = sum.add(new BigDecimal(waste.getTotalMoney()));
        }

        tv_money.setText(sum + "元");
        rubbishListAdapter = new RubbishListAdapter(FinishActivity.this, wasteList, MenuType.DELIVER);
        rubbishList.setAdapter(rubbishListAdapter);
        rubbishListAdapter.notifyDataSetChanged();

    }


    @Override
    protected void onResume() {
        super.onResume();
        timer.schedule(task, 1000, 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_finish:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra("status", "Finish");
                startActivity(intent);
                spfh.remove(phone);

                break;
            case R.id.btn_continue:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra("status", "Continue");
                intent.putExtra("phone", phone);
                startActivity(intent);
                break;
        }
    }

    //    倒计时方法
    public TimerTask task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recLen--;
                    tvTime.setText("" + recLen);
                    if (recLen <= 0) {
                        spfh.remove(phone);
                        startActivity(new Intent(FinishActivity.this, MainActivity.class));
                        timer.cancel();
                    }

                }
            });

        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        timer.cancel();
        finish();
    }
}
